package org.yxing.palpalgps.adapter;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.yxing.palpalgps.R;
import org.yxing.palpalgps.domain.AsyncImageLoader;
import org.yxing.palpalgps.http.AsyncRequest;
import org.yxing.palpalgps.http.ResponseException;
import org.yxing.palpalgps.oauth.OAuth;
import org.yxing.palpalgps.utils.Helper;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

/**
 * photos adapter
 * @author yuxing
 *
 */
public class PhotoAdapter extends BaseAdapter {
    
    private static final String TAG = "PhotoAdapter";
    
    /**
     * 保存图片id
     */
    private ArrayList<String> data;
    
    private Context mContext;
    
    private AsyncImageLoader imageLoader;
    
    private String uid;
    
    public PhotoAdapter(Context context, String uid) {
        this(context);
        this.uid = uid;
    }
    
    public PhotoAdapter(Context context) {
        mContext = context;
        data = new ArrayList<String>();
        imageLoader = new AsyncImageLoader();
    }

    public int getCount() {
        return data.size();
    }

    public String getItem(int position) {
        return data.get(position);
    }

    public long getItemId(int position) {
        return position;
    }

    /**
     * 
     */
    public View getView(int position, View convertView, ViewGroup parent) {
        //get view to show image
        convertView = LayoutInflater.from(mContext).inflate(R.layout.photo_grid_item, null);
        ImageView iv = (ImageView) convertView.findViewById(R.id.imageView1);
        imageLoader.loadDrawable(getImageUrl(getItem(position)), iv, callBack);
        return convertView;
    }
    
    /**
     * download image callback
     */
    private AsyncImageLoader.ImageCallback callBack = new AsyncImageLoader.ImageCallback() {
        
        public void imageLoaded(Drawable drawable, ImageView imageView,
                String imageUrl) {
            imageView.setImageDrawable(drawable);
        }
    };
    
    /**
     * 
     * @param pid photo id
     * @return string image url
     */
    private String getImageUrl(String pid) {
        return Helper.getSmallPictureUrl(pid);
    }
    
    /**
     * load data
     */
    public void loadData() {
        List<BasicNameValuePair> params = OAuth.createAuthParams();
        if (uid != null)
            params.add(new BasicNameValuePair("fuid", uid));
        
        AsyncRequest request = new AsyncRequest(mContext);
        request.fetch("photo/list", params, new AsyncRequest.FetchListener() {
            
            public void onError(int code, String msg) {
                
            }
            
            public void onComplete(JSONObject response) {
                data.clear();
                try {
                    JSONArray result = response.getJSONArray("result");
                    for (int i = 0; i < result.length(); i ++) {
                        JSONObject obj = result.getJSONObject(i);
                        data.add(obj.getString("pid"));
                    }
                    notifyDataSetChanged();
                } catch (JSONException e) {
                    Log.w(TAG, e.getMessage());
                }
            }
        });
    }
    
    public ArrayList<String> getItems() {
        return data;
    }
}
